package com.ruoyi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.service.OrderRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/hotel/baoShang")
public class BaoShangController {

    @Autowired
    ISysJobService jobService;

    @Autowired
    OrderRegisterService orderRegisterService;

    @CrossOrigin(origins = "*")
    @PostMapping("/setToken")
    public AjaxResult postEditLocalByOnline(@RequestBody Map map) {
        try {
            String code = map.get("code").toString();

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", code);
            jsonObject.put("password", "ylyh123456.");
            jsonObject.put("username", "17375354244");
            String sendJsonStr = JSONObject.toJSONString(jsonObject);

            // 调用宝尚登录接口
            String res = HttpUtils.sendPostJson("http://api.baoshanglvyou.com/auth/login", sendJsonStr);
            JSONObject jsonObject1 = JSONObject.parseObject(res);
            if (jsonObject1.getInteger("status") != null) {
                return AjaxResult.error(jsonObject1.getString("message"));
            }

            String token = "Bearer " + jsonObject1.getString("token");
            // 刷新宝尚token并开启定时任务
            orderRegisterService.updateBaoShangToken(token);
            SysJob sysJob = new SysJob();
            sysJob.setJobId(5L);
            sysJob.setStatus("0");
            jobService.changeStatus(sysJob);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error("未知错误");
    }
}
